
var oss_url = 'http://xhl.oss-cn-shanghai.aliyuncs.com/';
function checkValue(event,type) {
    var dom = $(event) ;
    var value = dom.val() ;

    if(value === null ){
        layer.msg('输入不能为空！',{
            skin:'winning-class',//自定义样式winning-class
            time:3000
        });
        $(event).focus();
        return false ;
    }
    switch (type){
        case "phone" :
            if(!(/^((\+\d{1,3}(-| )?\(?\d\)?(-| )?\d{1,5})|(\(?\d{2,6}\)?))(-| )?(\d{3,4})(-| )?(\d{4})(( x| ext)\d{1,5}){0,1}$/.test(value))){
                layer.msg('手机号输入有误！',{
                    skin:'winning-class',//自定义样式winning-class
                    time:3000
                });
                $(event).focus();
                return false;
            }
            break ;
        case "password":
            if(3 >value.length){
                layer.msg('密码不安全！',{
                    skin:'winning-class',//自定义样式winning-class
                    time:3000
                });
                $(event).focus();
                return false;
            }
            break ;
        case "number":
            if(!(/^\d+(\.\d+)?$/.test(value))  ){
                layer.msg('只能输入数字！',{
                    skin:'winning-class',//自定义样式winning-class
                    time:3000
                });
                $(event).focus();
            }
            break ;
    }

    return true ;
}

/*
  * @description :日期转换格式 输入日期样式
  * @author : peng huaneng
  * @date : 2018/3/1 15:16
  */
function formatDate(dateTime ,timeStyle) {
        var result = "" ;
        if(null == dateTime || ""==dateTime ){
            return "" ;
        }
        switch (timeStyle){
            case "yyyy/MM/dd":
                result = dateTime.split(" ")[0].replace(/-/g, "/") ;
                break ;
            case "yy/MM/dd HH:mm" :
               var data = dateTime.split(" ") ;
                result = data[0].replace(/-/g, "/") ;
                result =result + " " + data[1].substring(0,data[1].length-3) ;
                break ;
            case "yy/MM/dd&HH:mm" :
                var data = dateTime.split(" ") ;
                result = data[0].replace(/-/g, "/") ;
                result =result + "<br>" + data[1].substring(0,data[1].length-3) ;
                break ;
        }
       return result;

}
/*
    * @description : 隐藏提示信息
    * @params:
    * @author : peng huaneng
    * @date : 2018/3/7 16:05
    */
function tipsHidden() {
    setTimeout( function () {
        $(".input_placeholder").each(function () {

            if($(this).val() === ""){
                $(this).prev().css("display","block");
            }else{
                $(this).prev().css("display","none");
            }
        });
    },200) ;

}
/**
 * JSON格式化
 * @param array
 * @returns {*}
 */
function serializeJson(array) {
    var serializeObj = {};
    $(array).each(function () {
        if (serializeObj[this.name]) {
            if ($.isArray(serializeObj[this.name])) {
                serializeObj[this.name].push(this.value);
            } else {
                serializeObj[this.name] = [serializeObj[this.name], this.value];
            }
        } else {
            serializeObj[this.name] = this.value;
        }
    });
    return serializeObj;
}

function star(a){

	if(null == a || '' == a || undefined == a){
		a = 0;
	}
	var arr = a.toString().split('.');
	var num = parseInt(arr[0]);
	if(undefined == arr[1]){
		var fl = 0;
	}else{
		var fl  = 1;
	}

	var html = '';
	for(var i = 0;i<num;i++){
		html +='<div class="score_fill_img"></div>';
	}
	if(0 != fl){
		html +='<div class="score_half_img"></div>';
	}
	var h = 5 - num -fl;
	for(var j = 0;j<h;j++){
		html +='<div class="score_img"></div>';
	}
	return html;
}

$.fn.bootstrapTableEx = function ( opt ) {
    var defaults = {
        url : '' ,
        dataField : "rows" ,
        method : 'post' ,
        dataType : 'json' ,
        selectItemName : 'id' ,
        clickToSelect : true ,
        pagination : true ,
        smartDisplay : false ,
        pageNumber : 1 ,
        pageSize : 10 ,
        pageList : [ 1 , 5 , 10 , 20 ] ,
        paginationPreText : '上一页' ,
        paginationNextText : '下一页' ,
        sidePagination : 'server' ,
        queryParamsType : null ,
        columns : []
    }
    var option = $.extend( {} , defaults , opt );
    $( this ).bootstrapTable( option );
}

isNullOrEmpty = function ( obj ) {
    if ( (typeof (obj) == "string" && obj == "") || obj == null || obj == undefined ) {
        return true;
    } else {
        return false;
    }
}

dialogMsg = function ( msg , type ) {
    var msgType = {
        success : 1 ,
        error : 2 ,
        warn : 3 ,
        info : 7
    };
    if ( isNullOrEmpty( type ) ) {
        type = 'info';
    }
    top.layer.msg( msg , {
        skin:'winning-class',//自定义样式winning-class
        icon : msgType[ type ] ,
        time : 2000
    } );
}

checkedArray = function ( id ) {
    var isOK = true;
    if ( id == undefined || id == "" || id == 'null' || id == 'undefined' ) {
        isOK = false;
        dialogMsg( '您没有选中任何数据项！' );
    }
    return isOK;
}

checkedRow = function ( id ) {
    var isOK = true;
    if ( id == undefined || id == "" || id == 'null' || id == 'undefined' ) {
        isOK = false;
        dialogMsg( '您没有选中任何数据项！' );
    } else if ( id.length > 1 ) {
        isOK = false;
        dialogMsg( '您只能选择一条数据项！' );
    }
    return isOK;
}

function GetQueryString( name ) {
    var reg = new RegExp( "(^|&)" + name + "=([^&]*)(&|$)" );
    var r = window.location.search.substr( 1 ).match( reg );
    if ( r != null )return unescape( r[ 2 ] );
    return null;
}

/**
 * 自定义ajax
 * @param url
 * @param data
 * @param func
 * @param config
 */
function commonAjax(url, data, method, func) {
    var token = $api.getStorage('token');
    var supplier = $api.getStorage('id');
    if(method!=='login' || method!=='register'){
        data['supplier_id'] = supplier;
    }
    data['type'] = 'APP';
    if(method == 'getDataBase'){
        _loadingBaseShow();
    }else{
        //_loadingShow();
    }
    api.ajax({
        url: url,
        method: 'post',
        dataType: 'JSON',
        returnAll:false,
        headers: {
            token: token,
            from:'SHOPAPP',
            Method:method
        },
        data:{
            values:data
        }
    },function(ret,err){
        _loadingHide();
        func(ret,err);
    });
}


/**
 * @writer:杨建
 * @DateTime:2018-01-18
 * @rewriter:xx
 * @rewriter_date:YY-MM-DD
 * @description: 判断供应商资料审核状态
 */
function checkStatus(){
    var status = $api.getStorage('status');
    if(status==undefined||status=="undefined"){
        openwin('landing','登陆','landing',13)
        return false;
    }
    if(status==0){
        //跳转录入供应商信息页面
        openwin('authentication_information','认证信息','Authentication information',14,'提交','submit')
    }
    if(status==1) certification('audit');
    if(status==2) return true
    if(status==3) certification('fail');
}


function certification(name){
    api.openFrame({
        name: name,
        url: '../../html/my_shop/'+name+'.html',
        rect: {
            x: 0,
            y: 0,
            w: 'auto',
            h: 'auto'
        },
        bounces:false,
        pageParam: {
            name: name
        }
    });
}

/**
 * @writer:杨建
 * @DateTime:2018-01-26
 * @rewriter:xx
 * @rewriter_date:YY-MM-DD
 * @description: 改变订单状态接口
 */
function changeOrderStatus(orderId,tag){
    var url = indexUrl + 'changeOrderStatus';
    var data= {id:orderId,tag:tag}
    commonAjax(url, data,'changeOrderStatus',function(ret,err){
        if(ret){
            if(ret.status=='200'){
                init();
            }else{
                toastMsg(ret.msg);
            }
        }else{
            _httpErr(err);
        }
    })
}

/**
 * 用户信息缓存重置
 * @param {Object} info 供应商基础信息
 */
function setInfoStorage(info){
    $api.setStorage('id',info.id);
    $api.setStorage('name',info.name_english);
    $api.setStorage('name_e',info.name_english);
    $api.setStorage('address',info.address);
    $api.setStorage('address_e',info.address_english);
    $api.setStorage('tel',info.tel);
    $api.setStorage('business',info.business);
    $api.setStorage('business_e',info.business_english);
    $api.setStorage('company_e',info.company_english);
    $api.setStorage('company',info.business_english);
    $api.setStorage('company_code',info.company_code);
    $api.setStorage('is_open',info.is_open);
    $api.setStorage('shop_intro',info.shop_introduction);
    $api.setStorage('shop_intro_e',info.shop_introduction_english);
    $api.setStorage('manager',info.manager);
    $api.setStorage('manager_e',info.manager_english);
    $api.setStorage('manager_tel',info.manager_tel);
    $api.setStorage('email',info.email);
    $api.setStorage('token',info.app_token);
    $api.setStorage('post_code',info.post_code);
    $api.setStorage('image',info.imageaddr);
    $api.setStorage('send_type',info.send_time_type);
    $api.setStorage('manager_tel2',info.manager_tel2);
    $api.setStorage('manager_tel3',info.manager_tel3);
    $api.setStorage('country_id',info.country_id);
    $api.setStorage('city_id',info.city_id);
    $api.setStorage('town_id',info.town_id);
    $api.setStorage('status',info.check_status);
}

/**
 *  系统基础设置信息缓存设置
 */
function setStorageSetting(){

}


/**
 *	 暂无数据信息提示页面信息
 */
function noDataHtml(id){
    $api.html($api.byId(id), '');
    var html = '';
    html += '<div><div class="bgimg">';
    html += '<img src="../../icon/no-date.png" alt="" width="50px" height="50px" /></div>';
    html += '<div style="text-align: center"><p class="fontCh16">暂无数据</p>';
    html += '<p class="fontCh16">no data</p></div></div>';
    $api.html($api.byId(id),html);
}
/**
 *	 checkbox取值组合字符串
 */
function boxCombStr(domId){
    //循环所有多选框
    var str='';//构建用于删除的id字符串
    $(domId+" input[type='checkbox']").each(function(){
        var del=$(this).prop("checked");//判断按钮状态
        if(del){
            str+=$(this).val()+",";
        }
    });
    //判断str是否为空
    if(str.length>1){
        return str = str.substr(0,str.length - 1) ;
    }else{
        return '';
    }
}
/**
 *	 checkbox取值组合字符串
 */
function boxCombStrStatus(domId){
    //循环所有多选框
    var str='';//构建用于删除的id字符串
    $(domId+" input[type='checkbox']").each(function(){
        var del=$(this).prop("checked");//判断按钮状态
        if(del){
            str+=$(this).attr('status')+",";
        }
    });
    //判断str是否为空
    if(str.length>1){
        return str = str.substr(0,str.length - 1) ;
    }else{
        return '';
    }
}
//获取用户token
function getUserToken(){
    var suppliesId =localStorage.getItem('suppliesId');
    var web_token  =localStorage.getItem('web_token');
    $.ajax({
        type: "post",
        url: "/apishop/apishop/getUserToken",
        cache: false,
        data: {supplier_id:suppliesId},
        dataType: "json",
        success: function (res) {
            if (res.web_token != web_token){
                localStorage.removeItem('supplier_id');
                localStorage.removeItem('web_token');
                layer.msg('已在其他设备登录',{
                    skin:'winning-class',//自定义样式winning-class
                    time:3000
                });
                setTimeout(function () {
                    location.href = "/supplies/index/login.html";
                }, 2000);
            }
        }
    });
}

/**
 * 手机号检验
 * @param phone
 * @returns {boolean}
 */
function checkPhone(phone) {
    if (!(/^1(3|4|5|7|8)\d{9}$/.test(phone))) {
        layer.msg('请输入正确的电话号码格式</br>Please enter the correct format of the phone number',{
            skin:'winning-class',//自定义样式winning-class
            time:3000
        });
        return false;
    } else {
        return true;
    }
}
/**
 * 电话号码检验
 * @param tel
 * @returns {*}
 */
function checkTel(tel) {
    if (!(/^((0\d{2,3})-)(\d{7,8})(-(\d{3,}))?$/.test(tel))) {
        var dataType = checkPhone(tel);
        return dataType;
    } else {
        return true;
    }
}

/**
 *
 * @param email
 * @returns {boolean}
 */
function checkEmail(email) {
    var szReg=/^(\w)+(\.\w+)*@(\w)+((\.\w+)+)$/;
    if (!(szReg.test(emall))) {
        layer.msg('请输入正确的邮箱格式</br>Please enter the correct mailbox format',{
            skin:'winning-class',//自定义样式winning-class
            time:3000
        });
        return false;
    } else {
        return true;
    }
}